package net.halflite.mzkr.action;

import java.util.Map;

import javax.annotation.Resource;

import net.halflite.mzkr.annotation.Login;
import net.halflite.mzkr.form.LoginForm;
import net.halflite.mzkr.service.RegistedUserService;
import net.halflite.mzkr.type.LoginType;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionMessages;
import org.seasar.struts.annotation.ActionForm;
import org.seasar.struts.annotation.Execute;
import org.seasar.struts.enums.SaveType;

/** ログイン用アクションクラス */
public class LoginAction {

	@Resource
	private Map<String, Object> sessionScope;

	@Resource
	private RegistedUserService registedUserService;

	@ActionForm
	@Resource
	public LoginForm loginForm;

	/** ログイン情報入力 */
    @Execute(validator = false)
    public String index() {
        return "index.jsp";
    }

    /** ログイン処理 */
    @Execute(validate = "validate", saveErrors = SaveType.SESSION, input = "./?redirect=true")
    public String post() {
    	sessionScope.put(LoginType.USER.getKey(), registedUserService.login(loginForm));
        return "success?redirect=true";
    }

    /** ログイン完了 */
    @Login(LoginType.USER)
    @Execute(validator = false, removeActionForm = true)
    public String success() {
    	String backPath = (String) sessionScope.remove("backPath");
        return StringUtils.isEmpty(backPath) ? "success.jsp" : backPath + "?redirect=true";
    }
    
    /** ログインフォーム検証 */
    public ActionMessages validate() {
    	return registedUserService.validateLogin(loginForm);
    }
}
